// Namespace LyncServerExtension.Service: Service related functions.

// Namespace creation, and protection against global environment modification.
LyncServerExtension.Service = (function (ns, model) {

    ns.ServiceClient = function (endPoint) {
        this.call = function (operation, params, onSuccess, onFailure) {
            var paramString = '';
            for (key in params)
                paramString = paramString.concat(key, '=', escape(params[key]), '&');

            $.ajax({
                url: endPoint + '/' + operation + '?' + paramString,
                success: function (data, textStatus, jqXHR) {
                    if (onSuccess)
                        onSuccess(data);
                },
                error: function (jqXHR, ajaxOptions, thrownError) {
                    if (onFailure)
                        onFailure(thrownError);
                },
                dataType: 'json',
                cache: false
            });
        }
    }

    ns.LyncServiceProxy = function (culture) {
        var _endPointPath = '../WebService/LyncService.svc',
            _client = new ns.ServiceClient(_endPointPath),
            _culture = (culture) ? culture : '';

        this.listAuthorizedOwners = function (onSuccess, onFailure) {
            _client.call('ListAuthorizedOwners', { id: 'JSON', culture: _culture },
            handleResult(onSuccess, onFailure, processUsers), onFailure);
        }

        this.listAuthorizedDelegates = function (ownerSip, onSuccess, onFailure) {
            _client.call('ListAuthorizedDelegates', { id: 'JSON', culture: _culture, ownerSip: ownerSip },
            handleResult(onSuccess, onFailure, processUsers), onFailure);
        }

        this.listDelegates = function (ownerSip, onSuccess, onFailure) {
            _client.call('ListDelegates', { id: 'JSON', culture: _culture, ownerSip: ownerSip },
                handleResult(onSuccess, onFailure, processUsers), onFailure);
        }

        this.setCallForwardingDelegation = function (enabled, ownerSip, delegateSip, onSuccess, onFailure) {
            _client.call('SetCallForwardingDelegation',
                { id: 'JSON', culture: _culture, enabled: enabled, ownerSip: ownerSip, delegateSip: delegateSip },
                handleResult(onSuccess, onFailure), onFailure);
        }

        function processUsers(obj) {
            return obj.Users.map(makeUser);
        }

        function makeUser(u) {
            return new model.User(u.AccountName, u.Sip, u.FirstName, u.LastName, _endPointPath + '/GetUserPhoto?sip=' + u.Sip);
        }

        function handleResult(onSuccess, onFailure, process) {
            return function (obj) {
                if (obj.Type == 'LyncError')
                    onFailure(obj.Message);
                else
                    onSuccess(process ? process(obj) : obj);
            }
        }
    }

    return ns;
})({}, LyncServerExtension.Model);